package com.evenmo.tcp;

import com.sun.tools.javac.util.StringUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * This is Description
 * TCP服务端线程
 * @author moyongjun
 * @date 2019/11/24
 */
public class TCP_ServerThread implements Runnable {

    private Socket s =null;

    public TCP_ServerThread(Socket s){
        this.s=s;
    }
    @Override
    public void run() {
        try {
            //获取输入流，接受数据
            BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
            //获取输出流，输出数据
            PrintWriter pw =new PrintWriter(s.getOutputStream());

//            PrintStream ps =new PrintStream(s.getOutputStream());


            boolean flag=true;
            while (flag) {

                String str=br.readLine();

                if (str==null||"".equalsIgnoreCase(str) ||"bye".equalsIgnoreCase(str)) {
                    flag = !flag;
                }else{
                    String echo="echo:"+str;

                    System.out.println("传来的信息是："+str);

                    pw.println(echo);
                    pw.flush();
//                    ps.println(echo);
                }
            }
            s.close();
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
